//
//  ByCellCommitOrderProduct.m
//  itboye_shop_ios
//
//  Created by pro on 17/2/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellCommitOrderProduct.h"

@implementation ByCellCommitOrderProduct
{
    UIImageView *_mainView;             // 主图
    
    UILabel     *_titleLabel;           // 标题
    UILabel     *_skuLabel;             // 描述
    UILabel     *_priceLabel;           // 价格
    UILabel     *_countLabel;           // 数量
    
    UILabel     *_sepLine;              // 分割线
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = KShopCarCellBgColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupNormalUI];
    }
    return self;
}

- (void)setupNormalUI {
    
    // 分割线
    _sepLine = [[UILabel alloc] init];
    _sepLine.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_sepLine];
    
    // 主图
    _mainView = [[UIImageView alloc] init];
    [self.contentView addSubview:_mainView];
    
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 2;
    _titleLabel.font = FONT(14);
    _titleLabel.textColor = KShopCarCellMainTextColor;
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_titleLabel];
    
    // 描述
    _skuLabel = [[UILabel alloc] init];
    _skuLabel.numberOfLines = 2;
    _skuLabel.adjustsFontSizeToFitWidth = YES;
    _skuLabel.font = FONT(14);
    _skuLabel.textColor = KShopCarCellSubTextColor;
    _skuLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_skuLabel];
    
    // 价格
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.font = FONT(14);
    _priceLabel.textColor = KShopCarThemeColor;
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_priceLabel];
    
    // 创建数量视图
    _countLabel = [[UILabel alloc] init];
    _countLabel.userInteractionEnabled = YES;
    _countLabel.font = FONT(14);
    _countLabel.textColor = KShopCarCellMainTextColor;
    _countLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_countLabel];
    
    
}

- (void)setModel:(ByModelShopCarListItem *)model {
    
    _model = model;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat margin = 5;
    
    CGFloat x           = 0;
    CGFloat y           = 0;
    CGFloat width       = self.width;
    CGFloat height      = 2;
    // 分割线（白色）
    _sepLine.frame      = CGRectMake(x, y, width, height);
    
    // 主图
    x                   = BY_TABLE_LEFT_MARGIN;
    y                   = _sepLine.bottom + margin;
    width               = self.height -_sepLine.height - margin * 2;
    height              = width;
    _mainView.frame     = CGRectMake(x, y, width, height);
    [_mainView setImageWithURL:[NSURL URLWithString:_model.main_img] placeholderImage:DEFAULT_IMAGE];

    // 标题
    x                   = _mainView.right + 10;
    y                   = _mainView.top;
    width               = self.width - x - 10;
    height              = [_model.name getRectOfStringWithSize:CGSizeMake(width, 10000) font:14].size.height;
    height              = MIN(35, MAX(height, 20));
    _titleLabel.frame   = CGRectMake(x, y, width, height);
    
    // 规格
    x                   = _titleLabel.left;
    y                   = _titleLabel.bottom;
    width               = _titleLabel.width;
    if (_model.sku_desc.length == 0) {
        height          = 0;
    } else {
        height          = [_model.sku_desc getRectOfStringWithSize:CGSizeMake(width, 10000) font:14].size.height;
        height          = MIN(35, MAX(height, 20));
        
    }
    _skuLabel.frame     = CGRectMake(x, y, width, height);
    
    // 价格
    x                   = _skuLabel.left;
    y                   = 0;
    width               = _skuLabel.width / 2;
    height              = 20;
    _priceLabel.frame   = CGRectMake(_skuLabel.left, 0, _skuLabel.width / 2, 20);
    _priceLabel.bottom  = _mainView.bottom;
    
    // 数量
    x                   = _priceLabel.right;
    y                   = _priceLabel.top;
    width               = _priceLabel.width;
    height              = _priceLabel.height;
    _countLabel.frame   = CGRectMake(_priceLabel.right, _priceLabel.top, _priceLabel.width, 20);
    
    // 设置属性
    _titleLabel.text    = _model.name;
    _skuLabel.text      = _model.sku_desc;
    _priceLabel.text    = [NSString stringWithFormat:@"￥%.2f", _model.price.floatValue];
    _countLabel.text    = [NSString stringWithFormat:@"x %i", _model.count.intValue];
}

@end
